GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — react-native-firestack/lib/firestackModule.js at master · fullstackreact/react-native-firestack Expand file tree/ Copy pathfirestackModule.jsMore file actions 280 lines (239 loc) · 7.04 KB / Copy pathfirestackModule.jsFile metadata and controls280 lines (239 loc) · 7.04 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 import invariant from 'invariant' const createTypes = (prefix) => { const c = (str) => `${prefix.toUpperCase()}_${str.toUpperCase()}` return { ACTION_CALL: c('call_function'), ACTION_SUCCESS: c('call_function_success'), ACTION_FAIL: c('call_function_failure'), ACTION_LISTEN: c('listen'), ACTION_UNLISTEN: c('unlisten'), ACTION_REMOVE: c('remove'), ACTION_UPDATE: c('update'), ACTION_SET: c('set'), ACTION_GET: c('get'), ITEM_VALUE: c('value'), ITEM_ADDED: c('added'), ITEM_REMOVED: c('remove'), ITEM_CHANGED: c('changed'), UPDATED: c('updated') } } const defaultToObject = child => ({_key: child.key, ...child.val()}) const identity = (i) => i const defaultSortFn = (a, b) => a.timestamp < b.timestamp const defaultInitialState = { items: [], } export class FirestackModule { constructor(refName, opts={}) { invariant(refName && typeof refName !== 'undefined', 'No ref name passed'); this._refName = refName; this._makeRef = opts.makeRef || identity; const initialState = Object.assign({}, opts.initialState || defaultInitialState, { listening: false, items: [] }) this._localState = initialState; this._types = createTypes(this._refName); this._toObject = opts.toObject || defaultToObject this._sortFn = opts.sortFn || defaultSortFn this._onChange = opts.onChange || identity; if (opts.firestack) { this.setFirestack(opts.firestack); } else if (opts.store) { this.setStore(opts.store); } } makeRef(path) { const refName = [this._refName, path] const ref = this._firestack.database.ref(...refName); return this._makeRef(ref); } setFirestack(firestack) { if (firestack) { this._firestack = firestack; } } setStore(store) { if (store) { this._store = store; } } /* * Actions */ listen(cb) { let store = this._getStore(); invariant(store, 'Please set the store'); const T = this._types; const listenRef = this.makeRef(); const toObject = this._toObject; const _itemAdded = (snapshot, prevKey) => { const state = this._getState(); // local state const newItem = toObject(snapshot, state); let list = state.items || []; list.push(newItem) list = list.sort(this._sortFn) return this._handleUpdate(T.ITEM_ADDED, {items: list}, cb); } const _itemRemoved = (snapshot, prevKey) => { const state = this._getState(); // local state const itemKeys = state.items.map(i => i._key); const itemIndex = itemKeys.indexOf(snapshot.key); let newItems = [].concat(state.items); newItems.splice(itemIndex, 1); let list = newItems.sort(this._sortFn) return this._handleUpdate(T.ITEM_REMOVED, {items: list}, cb); } const _itemChanged = (snapshot, prevKey) => { const state = this._getState() const existingItem = toObject(snapshot, state); let list = state.items; let listIds = state.items.map(i => i._key); const itemIdx = listIds.indexOf(existingItem._key); list.splice(itemIdx, 1, existingItem); return this._handleUpdate(T.ITEM_CHANGED, {items: list}, cb); } return new Promise((resolve, reject) => { listenRef.on('child_added', _itemAdded); listenRef.on('child_removed', _itemRemoved); listenRef.on('child_changed', _itemChanged); this._handleUpdate(T.ACTION_LISTEN, null, (state) => { resolve(state) }) }) } unlisten() { const T = this._types; const ref = this.makeRef(); return new Promise((resolve, reject) => { ref.off() .then((success) => { this._handleUpdate(T.ACTION_UNLISTEN, null, (state) => { resolve(state) }) }); }) } // TODO: Untested getAt(path, cb) { const T = this._types; const ref = this.makeRef(path); const toObject = this._toObject; return new Promise((resolve, reject) => { ref.once('value', snapshot => { this._handleUpdate(T.ACTION_GET, null, (state) => { if (cb) { cb(toObject(snapshot, state)); } resolve(state) }) }, reject); }); } setAt(path, value, cb) { const T = this._types; const ref = this.makeRef(path); const toObject = this._toObject; return new Promise((resolve, reject) => { ref.setAt(value, (error) => { this._handleUpdate(T.ACTION_SET, null, (state) => { if (cb) { cb(error, value); } return error ? reject(error) : resolve(value) }); }) }); } updateAt(path, value, cb) { const T = this._types; const ref = this.makeRef(path); const toObject = this._toObject; return new Promise((resolve, reject) => { ref.updateAt(value, (error, snapshot) => { this._handleUpdate(T.ACTION_UPDATE, null, (state) => { if (cb) { cb(toObject(snapshot, state)); } return error ? reject(error) : resolve(value) }); }); }); } removeAt(path, cb) { const T = this._types; const ref = this.makeRef(path); const toObject = this._toObject; return new Promise((resolve, reject) => { ref.removeAt((error, snapshot) => { this._handleUpdate(T.ACTION_SET, null, (state) => { if (cb) { cb(toObject(snapshot, state)); } return error ? reject(error) : resolve(value) }); }); }); } // hackish, for now get actions() { const T = this._types; const wrap = (fn) => (...args) => { const params = args && args.length > 0 ? args : []; const promise = fn.bind(this)(...params) return {type: T.ACTION_CALL, payload: promise} } return [ 'listen', 'unlisten', 'getAt', 'setAt', 'updateAt', 'removeAt' ].reduce((sum, name) => { return { ...sum, [name]: wrap(this[name]) } }, {}) } get initialState() { return this._initialState; } get types() { return this._types } get reducer() { const T = this._types; return (state = this._localState, {type, payload, meta}) => { if (meta && meta.module && meta.module === this._refName) { switch (type) { case T.ACTION_LISTEN: return ({...state, listening: true}); case T.ACTION_UNLISTEN: return ({...state, listening: false}); default: return {...state, ...payload}; } } return state; } } /** * Helpers **/ _handleUpdate(type, newState = {}, cb = identity) { const store = this._getStore(); if (store && store.dispatch && typeof store.dispatch === 'function') { store.dispatch({type, payload: newState, meta: { module: this._refName }}) } return cb(newState); } _getStore() { return this._store ? this._store : (this._firestack ? this._firestack.store : null); } _getState() { const store = this._getStore(); return store.getState()[this._refName]; } } export default FirestackModule |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |