Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions src/Meteor.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import Mongo from './Mongo';
import { Collection, runObservers, localCollections } from './Collection';
import call from './Call';

import withTracker from './components/ReactMeteorData';
import withTracker from './components/withTracker';
import useTracker from './components/useTracker';

import ReactiveDict from './ReactiveDict';

Expand All @@ -42,6 +43,7 @@ module.exports = {
return new Collection(name, options);
},
withTracker,
useTracker,
getData() {
return Data;
},
Expand Down Expand Up @@ -84,7 +86,7 @@ module.exports = {
if((!endpoint.startsWith("ws") || !endpoint.endsWith("/websocket")) && !options.suppressUrlErrors) {
throw new Error(`Your url "${endpoint}" may be in the wrong format. It should start with "ws://" or "wss://" and end with "/websocket", e.g. "wss://myapp.meteor.com/websocket". To disable this warning, connect with option "suppressUrlErrors" as true, e.g. Meteor.connect("${endpoint}", {suppressUrlErrors:true});`);
}

if (!options.AsyncStorage) {
const AsyncStorage = require('@react-native-community/async-storage').default;

Expand Down Expand Up @@ -155,9 +157,9 @@ module.exports = {
_id: message.id,
...message.fields,
};

Data.db[message.collection].upsert(document);

runObservers("added", message.collection, document);
});

Expand Down Expand Up @@ -192,18 +194,18 @@ module.exports = {
...message.fields,
...unset,
};

const oldDocument = Data.db[message.collection].findOne({_id:message.id});

Data.db[message.collection].upsert(document);
runObservers("changed", message.collection, document, oldDocument);

runObservers("changed", message.collection, document, oldDocument);
}
});

Data.ddp.on('removed', message => {
if(Data.db[message.collection]) {
const oldDocument = Data.db[message.collection].findOne({_id:message.id});
const oldDocument = Data.db[message.collection].findOne({_id:message.id});
Data.db[message.collection].del(message.id);
runObservers("removed", message.collection, oldDocument);
}
Expand Down
118 changes: 0 additions & 118 deletions src/components/MeteorDataManager.js

This file was deleted.

97 changes: 0 additions & 97 deletions src/components/ReactMeteorData.js

This file was deleted.

30 changes: 30 additions & 0 deletions src/components/useTracker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { useEffect, useState } from 'react';
import Tracker from 'trackr';
import Data from '../Data';

export default (trackerFn, deps = []) => {
const [response, setResponse] = useState(trackerFn());
const meteorDataDep = new Tracker.Dependency();
let computation = null;
const dataChangedCallback = () => {
meteorDataDep.changed();
};

const stopComputation = () => {
computation && computation.stop();
computation = null;
};

Data.onChange(dataChangedCallback);

useEffect(() => {
stopComputation();
Tracker.autorun(currentComputation => {
meteorDataDep.depend();
computation = currentComputation;
setResponse(trackerFn());
});
return () => { stopComputation(); Data.offChange(dataChangedCallback); };
}, deps);
return response;
};
16 changes: 16 additions & 0 deletions src/components/withTracker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import React, { forwardRef, memo } from 'react';
import useTracker from './useTracker';

export default function withTracker (options) {
return Component => {
const expandedOptions = typeof options === 'function' ? { getMeteorData: options } : options;
const { getMeteorData, pure = true } = expandedOptions;

const WithTracker = forwardRef((props, ref) => {
const data = useTracker(() => getMeteorData(props) || {});
return <Component ref={ref} {...props} {...data} />;
});

return pure ? memo(WithTracker) : WithTracker;
};
}