Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Ember.Evented for own implementation #390

Merged
merged 12 commits into from
Sep 16, 2019
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module.exports = {
root: true,
parser: 'babel-eslint',
parserOptions: {
ecmaVersion: 2018,
sourceType: 'module'
Expand Down
185 changes: 185 additions & 0 deletions addon/-private/defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
export const DEFAULTS = {
/**
Increases or decreases depending on scroll direction

@private
@property currentPage
@type Integer
@default 0
*/
currentPage: 0,

/**
@private
@property extraParams
@type Object
@default null
*/
extraParams: null,

/**
Used as a marker for the page the route starts on

@private
@property firstPage
@type Integer
@default 0
*/
firstPage: 0,

/**
@public
@property isError
@type Boolean
@default false
*/
isError: false,

/**
@public
@property isLoaded
@type Boolean
@default false
*/
isLoaded: false,

/**
@public
@property loadingMore
@type Boolean
@default false
*/
loadingMore: false,

/**
Arbitrary meta copied over from
the HTTP response, to maintain the
default behavior of ember-data requests
@type objects
@default null
*/
meta: null,

/**
@private
@property _perPage
@type Integer
@default 25
*/
perPage: 25,

/**
@public
@property reachedInfinity
@default false
*/
reachedInfinity: false,

/**
@public
@property store
@default null
*/
store: null,

/**
Name of the "per page" param in the
resource request payload
@type {String}
@default "per_page"
*/
perPageParam: 'per_page',

/**
Name of the "page" param in the
resource request payload
@type {String}
@default "page"
*/
pageParam: 'page',

/**
Path of the "total pages" param in
the HTTP response
@type {String}
@default "meta.total_pages"
*/
totalPagesParam: 'meta.total_pages',

/**
Path of the "count" param in indicating
number of records from HTTP response
@type {String}
@default "meta.count"
*/
countParam: 'meta.count',

/**
The supported findMethod name for
the developers Ember Data version.
Provided here for backwards compat.
@public
@property storeFindMethod
@default null
*/
storeFindMethod: null,

/**
@private
@property _count
@type Integer
@default 0
*/
_count: 0,

/**
@private
@property _totalPages
@type Integer
@default 0
*/
_totalPages: 0,

/**
@private
@property _infinityModelName
@type String
@default null
*/
_infinityModelName: null,

/**
@private
@property _firstPageLoaded
@type Boolean
@default false
*/
_firstPageLoaded: false,

/**
@private
@property _increment
@type Integer
@default 1
*/
_increment: 1,

/**
simply used for previous page scrolling abilities and passed from
infinity-loader component and set on infinityModel
@private
@property _scrollable
@type Integer
@default null
*/
_scrollable: null,

/**
determines if can load next page or previous page (if applicable)

@private
@property _canLoadMore
@type Boolean
*/
_canLoadMore: null
}
38 changes: 38 additions & 0 deletions addon/-private/evented.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Notifier from './notifier';

// in lieue of a decorator, lets just use Mixin/composition pattern
export function addEvented(Base) {
return class extends Base {
on(eventName, listener) {
return notifierForEvent(this, eventName).addListener(listener);
}

off(eventName, listener) {
return notifierForEvent(this, eventName).removeListener(listener);
}

trigger(eventName, ...args) {
const notifier = notifierForEvent(this, eventName);
if (notifier) {
notifier.trigger.apply(notifier, args);
}
}
}
}

function notifierForEvent(
object,
eventName
) {
if (object._eventedNotifiers === undefined) {
object._eventedNotifiers = {};
}

let notifier = object._eventedNotifiers[eventName];

if (!notifier) {
notifier = object._eventedNotifiers[eventName] = new Notifier();
}

return notifier;
}
36 changes: 36 additions & 0 deletions addon/-private/notifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
export default class Notifier {
constructor() {
this.listeners = [];
}

/**
* Add a callback as a listener, which will be triggered when sending
* notifications.
*/
addListener(listener) {
this.listeners.push(listener);

return () => this.removeListener(listener);
}

/**
* Remove a listener so that it will no longer receive notifications.
*/
removeListener(listener) {
const listeners = this.listeners;

for (let i = 0, len = listeners.length; i < len; i++) {
if (listeners[i] === listener) {
listeners.splice(i, 1);
return;
}
}
}

/**
* Notify registered listeners.
*/
trigger(...args) {
this.listeners.slice(0).forEach(listener => listener(...args));
}
}
6 changes: 3 additions & 3 deletions addon/components/infinity-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ const InfinityLoaderComponent = Component.extend(InViewportMixin, {

get(this, 'infinityModelContent')
.then((infinityModel) => {
infinityModel.off('infinityModelLoaded', this, this._loadStatusDidChange);
infinityModel.off('infinityModelLoaded', this, this._loadStatusDidChange.bind(this));
});

this.removeObserver('infinityModel', this, this._initialInfinityModelSetup);
Expand Down Expand Up @@ -166,7 +166,7 @@ const InfinityLoaderComponent = Component.extend(InViewportMixin, {
_initialInfinityModelSetup() {
get(this, 'infinityModelContent')
.then((infinityModel) => {
infinityModel.on('infinityModelLoaded', this, this._loadStatusDidChange);
infinityModel.on('infinityModelLoaded', this._loadStatusDidChange.bind(this));
set(infinityModel, '_scrollable', get(this, 'scrollable'));
set(this, 'isDoneLoading', false);
if (!get(this, 'hideOnInfinity')) {
Expand Down Expand Up @@ -238,7 +238,7 @@ const InfinityLoaderComponent = Component.extend(InViewportMixin, {
// service action
get(this, 'infinity').infinityLoad(content, 1)
.then(() => {
if (get(content, '_canLoadMore')) {
if (get(content, 'canLoadMore')) {
this._checkScrollableHeight();
}
});
Expand Down
Loading