lib/probes/FileSyncProbe.js
function(error, content) {

          var isInitializing = (callback !== null),
              initCallback = callback;
          callback = null;

          if (error && error.code === 'ENOENT') {
            // File doesn't exist. Set the model to null.
            t.set({model: {}}, {silent: isInitializing});
            // Convert the code from the sync probe spec
            error.code = 'NOTFOUND';
          }
          if (error) {
            if (isInitializing) {
              t.release();
              var err = {code: error.code, msg: 'LiveSync requires the file to exist and be readable'};
              initCallback(err);
            }
            return;
          }

          // Parse the JSON content into a JS object.
          try {
            content = JSON.parse(content);
            logger.info('fileParse', {id: t.get('modelId'), content: content});
          } catch (e) {

            // Fail the probe on first load error
            if (isInitializing) {
              t.release();
              initCallback({code: 'BAD_FORMAT', msg: 'Non-JSON formatted file'});
            }

            // Nothing productive to do if the file can't be parsed. Just log it.
            logger.error('fileParse', {error: e, id: t.get('modelId'), content: content});
            return;
          }

          // Set the content into the model if it's different
          // Have to compare raw objects because toJSON returns deep references to models
          var priorModel = t.get('model');
          if (!priorModel || !_.isEqual(content, JSON.parse(JSON.stringify(priorModel)))) {
            t.set({model: content}, {silent: isInitializing});
          }

          // Call the initialization callback on first load
          if (isInitializing) {
            initCallback();
          }
        }
function(error, content) {

          var isInitializing = (callback !== null),
              initCallback = callback;
          callback = null;

          if (error && error.code === 'ENOENT') {
            // File doesn't exist. Set the model to null.
            t.set({model: {}}, {silent: isInitializing});
            // Convert the code from the sync probe spec
            error.code = 'NOTFOUND';
          }
          if (error) {
            if (isInitializing) {
              t.release();
              var err = {code: error.code, msg: 'LiveSync requires the file to exist and be readable'};
              initCallback(err);
            }
            return;
          }

          // Parse the JSON content into a JS object.
          try {
            content = JSON.parse(content);
            logger.info('fileParse', {id: t.get('modelId'), content: content});
          } catch (e) {

            // Fail the probe on first load error
            if (isInitializing) {
              t.release();
              initCallback({code: 'BAD_FORMAT', msg: 'Non-JSON formatted file'});
            }

            // Nothing productive to do if the file can't be parsed. Just log it.
            logger.error('fileParse', {error: e, id: t.get('modelId'), content: content});
            return;
          }

          // Set the content into the model if it's different
          // Have to compare raw objects because toJSON returns deep references to models
          var priorModel = t.get('model');
          if (!priorModel || !_.isEqual(content, JSON.parse(JSON.stringify(priorModel)))) {
            t.set({model: content}, {silent: isInitializing});
          }

          // Call the initialization callback on first load
          if (isInitializing) {
            initCallback();
          }
        }
function ( error , content ) { var isInitializing = ( callback !== null ) , initCallback = callback ; callback = null ; if ( error && error . code === 'ENOENT' ) { // File doesn't exist. Set the model to null. t . set ( { model : { } } , { silent : isInitializing } ) ; // Convert the code from the sync probe spec error . code = 'NOTFOUND' ; } if ( error ) { if ( isInitializing ) { t . release ( ) ; var err = { code : error . code , msg : 'LiveSync requires the file to exist and be readable' } ; initCallback ( err ) ; } return ; } // Parse the JSON content into a JS object. try { content = JSON . parse ( content ) ; logger . info ( 'fileParse' , { id : t . get ( 'modelId' ) , content : content } ) ; } catch ( e ) { // Fail the probe on first load error if ( isInitializing ) { t . release ( ) ; initCallback ( { code : 'BAD_FORMAT' , msg : 'Non-JSON formatted file' } ) ; } // Nothing productive to do if the file can't be parsed. Just log it. logger . error ( 'fileParse' , { error : e , id : t . get ( 'modelId' ) , content : content } ) ; return ; } // Set the content into the model if it's different // Have to compare raw objects because toJSON returns deep references to models var priorModel = t . get ( 'model' ) ; if ( ! priorModel || ! _ . isEqual ( content , JSON . parse ( JSON . stringify ( priorModel ) ) ) ) { t . set ( { model : content } , { silent : isInitializing } ) ; } // Call the initialization callback on first load if ( isInitializing ) { initCallback ( ) ; } }
Build the function to watch the file
Build the function to watch the file