@knocklabs/client
Advanced tools
Comparing version 0.3.3 to 0.3.4
@@ -275,3 +275,6 @@ "use strict"; | ||
response, | ||
opts, | ||
_opts, | ||
_args7 = arguments; | ||
return _regenerator["default"].wrap(function _callee7$(_context7) { | ||
@@ -319,13 +322,18 @@ while (1) { | ||
if (options.before) { | ||
// We were doing a before fetch, so prepend these | ||
opts = { | ||
shouldSetPage: false, | ||
shouldAppend: true | ||
}; | ||
setState(function (state) { | ||
return state.prependItems(response); | ||
return state.setResult(response, opts); | ||
}); | ||
} else if (options.after) { | ||
// Append items as fetching after | ||
_opts = { | ||
shouldSetPage: true, | ||
shouldAppend: true | ||
}; | ||
setState(function (state) { | ||
return state.appendItems(response); | ||
return state.setResult(response, _opts); | ||
}); | ||
} else { | ||
// Otherwise just clobber everything in here | ||
setState(function (state) { | ||
@@ -357,2 +365,42 @@ return state.setResult(response); | ||
}, { | ||
key: "fetchNextPage", | ||
value: function () { | ||
var _fetchNextPage = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8() { | ||
var getState, _getState, pageInfo; | ||
return _regenerator["default"].wrap(function _callee8$(_context8) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
case 0: | ||
// Attempts to fetch the next page of results (if we have any) | ||
getState = this.store.getState; | ||
_getState = getState(), pageInfo = _getState.pageInfo; | ||
if (pageInfo.after) { | ||
_context8.next = 4; | ||
break; | ||
} | ||
return _context8.abrupt("return"); | ||
case 4: | ||
this.fetch({ | ||
after: pageInfo.after | ||
}); | ||
case 5: | ||
case "end": | ||
return _context8.stop(); | ||
} | ||
} | ||
}, _callee8, this); | ||
})); | ||
function fetchNextPage() { | ||
return _fetchNextPage.apply(this, arguments); | ||
} | ||
return fetchNextPage; | ||
}() | ||
}, { | ||
key: "broadcast", | ||
@@ -366,8 +414,8 @@ value: function broadcast(eventName, data) { | ||
value: function () { | ||
var _onNewMessageReceived = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee8(_ref) { | ||
var _onNewMessageReceived = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9(_ref) { | ||
var metadata, _this$store, getState, setState, currentHead; | ||
return _regenerator["default"].wrap(function _callee8$(_context8) { | ||
return _regenerator["default"].wrap(function _callee9$(_context9) { | ||
while (1) { | ||
switch (_context8.prev = _context8.next) { | ||
switch (_context9.prev = _context9.next) { | ||
case 0: | ||
@@ -389,6 +437,6 @@ metadata = _ref.metadata; | ||
case "end": | ||
return _context8.stop(); | ||
return _context9.stop(); | ||
} | ||
} | ||
}, _callee8, this); | ||
}, _callee9, this); | ||
})); | ||
@@ -418,4 +466,4 @@ | ||
if (badgeCountAttr) { | ||
var _getState = getState(), | ||
metadata = _getState.metadata; | ||
var _getState2 = getState(), | ||
metadata = _getState2.metadata; | ||
@@ -436,10 +484,10 @@ var direction = type.startsWith("un") ? itemIds.length : -itemIds.length; | ||
value: function () { | ||
var _makeStatusUpdate = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee9(itemOrItems, type) { | ||
var _makeStatusUpdate = (0, _asyncToGenerator2["default"])( /*#__PURE__*/_regenerator["default"].mark(function _callee10(itemOrItems, type) { | ||
var itemIds, result; | ||
return _regenerator["default"].wrap(function _callee9$(_context9) { | ||
return _regenerator["default"].wrap(function _callee10$(_context10) { | ||
while (1) { | ||
switch (_context9.prev = _context9.next) { | ||
switch (_context10.prev = _context10.next) { | ||
case 0: | ||
if (!Array.isArray(itemOrItems)) { | ||
_context9.next = 5; | ||
_context10.next = 5; | ||
break; | ||
@@ -451,3 +499,3 @@ } | ||
}); | ||
_context9.next = 4; | ||
_context10.next = 4; | ||
return this.apiClient.makeRequest({ | ||
@@ -462,6 +510,6 @@ method: "POST", | ||
case 4: | ||
return _context9.abrupt("return", _context9.sent); | ||
return _context10.abrupt("return", _context10.sent); | ||
case 5: | ||
_context9.next = 7; | ||
_context10.next = 7; | ||
return this.apiClient.makeRequest({ | ||
@@ -473,11 +521,11 @@ method: type.startsWith("un") ? "DELETE" : "PUT", | ||
case 7: | ||
result = _context9.sent; | ||
return _context9.abrupt("return", result); | ||
result = _context10.sent; | ||
return _context10.abrupt("return", result); | ||
case 9: | ||
case "end": | ||
return _context9.stop(); | ||
return _context10.stop(); | ||
} | ||
} | ||
}, _callee9, this); | ||
}, _callee10, this); | ||
})); | ||
@@ -484,0 +532,0 @@ |
@@ -12,6 +12,6 @@ "use strict"; | ||
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); | ||
var _vanilla = _interopRequireDefault(require("zustand/vanilla")); | ||
var _utils = require("./utils"); | ||
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } | ||
@@ -21,8 +21,13 @@ | ||
function sortItems(items) { | ||
return items.sort(function (a, b) { | ||
return new Date(b.inserted_at).getTime() - new Date(a.inserted_at).getTime(); | ||
}); | ||
function processItems(items) { | ||
var deduped = (0, _utils.deduplicateItems)(items); | ||
var sorted = (0, _utils.sortItems)(deduped); | ||
return sorted; | ||
} | ||
var defaultSetResultOptions = { | ||
shouldSetPage: true, | ||
shouldAppend: false | ||
}; | ||
function createStore() { | ||
@@ -33,2 +38,3 @@ return (0, _vanilla["default"])(function (set) { | ||
loading: false, | ||
// Keeps track of the current badge counts | ||
metadata: { | ||
@@ -39,2 +45,8 @@ total_count: 0, | ||
}, | ||
// Keeps track of the last full page of results we received (for paginating) | ||
pageInfo: { | ||
before: null, | ||
after: null, | ||
page_size: 50 | ||
}, | ||
setLoading: function setLoading(loading) { | ||
@@ -49,20 +61,12 @@ return set(function () { | ||
var entries = _ref.entries, | ||
meta = _ref.meta; | ||
return set(function () { | ||
return { | ||
items: sortItems(entries), | ||
metadata: meta, | ||
loading: false | ||
}; | ||
}); | ||
}, | ||
prependItems: function prependItems(_ref2) { | ||
var entries = _ref2.entries, | ||
meta = _ref2.meta; | ||
meta = _ref.meta, | ||
page_info = _ref.page_info; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultSetResultOptions; | ||
return set(function (state) { | ||
var newItems = state.items; | ||
newItems.unshift.apply(newItems, (0, _toConsumableArray2["default"])(entries)); | ||
// We resort the list on set, so concating everything is fine (if a bit suboptimal) | ||
var items = options.shouldAppend ? processItems(state.items.concat(entries)) : entries; | ||
return { | ||
items: sortItems(newItems), | ||
items: items, | ||
metadata: meta, | ||
pageInfo: options.shouldSetPage ? page_info : state.pageInfo, | ||
loading: false | ||
@@ -72,14 +76,2 @@ }; | ||
}, | ||
appendItems: function appendItems(_ref3) { | ||
var entries = _ref3.entries, | ||
meta = _ref3.meta; | ||
return set(function (state) { | ||
var newItems = state.items.concat(entries); | ||
return { | ||
items: sortItems(newItems), | ||
metadata: meta, | ||
loading: false | ||
}; | ||
}); | ||
}, | ||
setMetadata: function setMetadata(metadata) { | ||
@@ -86,0 +78,0 @@ return set(function () { |
@@ -192,9 +192,14 @@ import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator"; | ||
if (options.before) { | ||
// We were doing a before fetch, so prepend these | ||
setState(state => state.prependItems(response)); | ||
var opts = { | ||
shouldSetPage: false, | ||
shouldAppend: true | ||
}; | ||
setState(state => state.setResult(response, opts)); | ||
} else if (options.after) { | ||
// Append items as fetching after | ||
setState(state => state.appendItems(response)); | ||
var _opts = { | ||
shouldSetPage: true, | ||
shouldAppend: true | ||
}; | ||
setState(state => state.setResult(response, _opts)); | ||
} else { | ||
// Otherwise just clobber everything in here | ||
setState(state => state.setResult(response)); | ||
@@ -212,2 +217,25 @@ } | ||
fetchNextPage() { | ||
var _this8 = this; | ||
return _asyncToGenerator(function* () { | ||
// Attempts to fetch the next page of results (if we have any) | ||
var { | ||
getState | ||
} = _this8.store; | ||
var { | ||
pageInfo | ||
} = getState(); | ||
if (!pageInfo.after) { | ||
// Nothing more to fetch | ||
return; | ||
} | ||
_this8.fetch({ | ||
after: pageInfo.after | ||
}); | ||
})(); | ||
} | ||
broadcast(eventName, data) { | ||
@@ -219,3 +247,3 @@ this.broadcaster.emit(eventName, data); | ||
onNewMessageReceived(_ref) { | ||
var _this8 = this; | ||
var _this9 = this; | ||
@@ -230,3 +258,3 @@ return _asyncToGenerator(function* () { | ||
setState | ||
} = _this8.store; | ||
} = _this9.store; | ||
var currentHead = getState().items[0]; // Optimistically set the badge counts | ||
@@ -236,3 +264,3 @@ | ||
_this8.fetch({ | ||
_this9.fetch({ | ||
before: currentHead === null || currentHead === void 0 ? void 0 : currentHead.__cursor | ||
@@ -269,3 +297,3 @@ }); | ||
makeStatusUpdate(itemOrItems, type) { | ||
var _this9 = this; | ||
var _this10 = this; | ||
@@ -276,3 +304,3 @@ return _asyncToGenerator(function* () { | ||
var itemIds = itemOrItems.map(item => item.id); | ||
return yield _this9.apiClient.makeRequest({ | ||
return yield _this10.apiClient.makeRequest({ | ||
method: "POST", | ||
@@ -287,3 +315,3 @@ url: "/v1/messages/batch/".concat(type), | ||
var result = yield _this9.apiClient.makeRequest({ | ||
var result = yield _this10.apiClient.makeRequest({ | ||
method: type.startsWith("un") ? "DELETE" : "PUT", | ||
@@ -290,0 +318,0 @@ url: "/v1/messages/".concat(itemOrItems.id, "/").concat(type) |
@@ -8,9 +8,14 @@ import _defineProperty from "@babel/runtime/helpers/defineProperty"; | ||
import create from "zustand/vanilla"; | ||
import { deduplicateItems, sortItems } from "./utils"; | ||
function sortItems(items) { | ||
return items.sort((a, b) => { | ||
return new Date(b.inserted_at).getTime() - new Date(a.inserted_at).getTime(); | ||
}); | ||
function processItems(items) { | ||
var deduped = deduplicateItems(items); | ||
var sorted = sortItems(deduped); | ||
return sorted; | ||
} | ||
var defaultSetResultOptions = { | ||
shouldSetPage: true, | ||
shouldAppend: false | ||
}; | ||
export default function createStore() { | ||
@@ -20,2 +25,3 @@ return create(set => ({ | ||
loading: false, | ||
// Keeps track of the current badge counts | ||
metadata: { | ||
@@ -26,27 +32,25 @@ total_count: 0, | ||
}, | ||
// Keeps track of the last full page of results we received (for paginating) | ||
pageInfo: { | ||
before: null, | ||
after: null, | ||
page_size: 50 | ||
}, | ||
setLoading: loading => set(() => ({ | ||
loading | ||
})), | ||
setResult: _ref => { | ||
setResult: function setResult(_ref) { | ||
var { | ||
entries, | ||
meta | ||
meta, | ||
page_info | ||
} = _ref; | ||
return set(() => ({ | ||
items: sortItems(entries), | ||
metadata: meta, | ||
loading: false | ||
})); | ||
}, | ||
prependItems: _ref2 => { | ||
var { | ||
entries, | ||
meta | ||
} = _ref2; | ||
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultSetResultOptions; | ||
return set(state => { | ||
var newItems = state.items; | ||
newItems.unshift(...entries); | ||
// We resort the list on set, so concating everything is fine (if a bit suboptimal) | ||
var items = options.shouldAppend ? processItems(state.items.concat(entries)) : entries; | ||
return { | ||
items: sortItems(newItems), | ||
items, | ||
metadata: meta, | ||
pageInfo: options.shouldSetPage ? page_info : state.pageInfo, | ||
loading: false | ||
@@ -56,16 +60,2 @@ }; | ||
}, | ||
appendItems: _ref3 => { | ||
var { | ||
entries, | ||
meta | ||
} = _ref3; | ||
return set(state => { | ||
var newItems = state.items.concat(entries); | ||
return { | ||
items: sortItems(newItems), | ||
metadata: meta, | ||
loading: false | ||
}; | ||
}); | ||
}, | ||
setMetadata: metadata => set(() => ({ | ||
@@ -72,0 +62,0 @@ metadata |
@@ -91,2 +91,3 @@ import { StoreApi } from "zustand"; | ||
}>; | ||
fetchNextPage(): Promise<void>; | ||
private broadcast; | ||
@@ -93,0 +94,0 @@ private onNewMessageReceived; |
@@ -0,9 +1,13 @@ | ||
import { PageInfo } from "../../interfaces"; | ||
import { FeedItem, FeedMetadata, FeedResponse } from "./interfaces"; | ||
export declare type StoreFeedResultOptions = { | ||
shouldSetPage?: boolean; | ||
shouldAppend?: boolean; | ||
}; | ||
export declare type FeedStoreState = { | ||
items: FeedItem[]; | ||
pageInfo: PageInfo; | ||
metadata: FeedMetadata; | ||
loading: boolean; | ||
setResult: (response: FeedResponse) => void; | ||
prependItems: (response: FeedResponse) => void; | ||
appendItems: (response: FeedResponse) => void; | ||
setResult: (response: FeedResponse, opts?: StoreFeedResultOptions) => void; | ||
setMetadata: (metadata: FeedMetadata) => void; | ||
@@ -10,0 +14,0 @@ setLoading: (loading: boolean) => void; |
{ | ||
"name": "@knocklabs/client", | ||
"version": "0.3.3", | ||
"version": "0.3.4", | ||
"description": "The clientside library for interacting with Knock", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/knocklabs/knock-client-js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
138958
63
1637