Skip to content
Open
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
16 changes: 10 additions & 6 deletions Pilot.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
});
})(typeof define === 'function' && define.amd ? define : function (deps, callback) {
window.Pilot = callback(window.Emitter);
}, function (define) {
define('src/querystring',[], function () {
}, function (define) {define('src/querystring',[], function () {
'use strict';

var encodeURIComponent = window.encodeURIComponent;
Expand Down Expand Up @@ -479,14 +478,19 @@ define('src/match',[], function () {

define('src/action-queue',['Emitter'], function(Emitter) {

function ActionQueue() {
function ActionQueue(options) {
Emitter.apply(this);

this._queue = [];
this._activeIds = {};
this._id = 0;
this._lastQueueItem = void 0;
this._endedCount = -1;

this._options = {
// Включает режим выполнения всех без исключения экшнов без блокировок
forceParallel: options.forceParallel
}
}

ActionQueue.PRIORITY_HIGH = 1;
Expand All @@ -499,7 +503,7 @@ define('src/action-queue',['Emitter'], function(Emitter) {
// TODO: arg types check

// Проставляем по умолчанию наивысший приоритет
if (action.priority == null) {
if (action.priority == null || this._options.forceParallel) {
action.priority = ActionQueue.PRIORITY_HIGH;
}

Expand Down Expand Up @@ -662,7 +666,7 @@ define('src/loader',['./match', './action-queue'], function (match, ActionQueue)
// Дебаг-режим, выводит в performance все экшны
this._debug = false;
// Очередь экшнов
this._actionQueue = new ActionQueue();
this._actionQueue = new ActionQueue(this._options);

this.names.forEach(function (name) {
this._index[name] = _cast(name, models[name]);
Expand Down Expand Up @@ -2087,4 +2091,4 @@ define('src/pilot.js',[
return Pilot;
});

});
});
9 changes: 7 additions & 2 deletions src/action-queue.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
define(['Emitter'], function(Emitter) {

function ActionQueue() {
function ActionQueue(options) {
Emitter.apply(this);

this._queue = [];
this._activeIds = {};
this._id = 0;
this._lastQueueItem = void 0;
this._endedCount = -1;

this._options = {
// Включает режим выполнения всех без исключения экшнов без блокировок
forceParallel: options.forceParallel
}
}

ActionQueue.PRIORITY_HIGH = 1;
Expand All @@ -20,7 +25,7 @@ define(['Emitter'], function(Emitter) {
// TODO: arg types check

// Проставляем по умолчанию наивысший приоритет
if (action.priority == null) {
if (action.priority == null || this._options.forceParallel) {
action.priority = ActionQueue.PRIORITY_HIGH;
}

Expand Down
2 changes: 1 addition & 1 deletion src/loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ define(['./match', './action-queue'], function (match, ActionQueue) {
// Дебаг-режим, выводит в performance все экшны
this._debug = false;
// Очередь экшнов
this._actionQueue = new ActionQueue();
this._actionQueue = new ActionQueue(this._options);

this.names.forEach(function (name) {
this._index[name] = _cast(name, models[name]);
Expand Down
17 changes: 15 additions & 2 deletions tests/loader.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function sleep(milliseconds) {
}

// Этот загрузчик любит спать и писать об этом в лог
async function createSleepyLoggingLoader(log, {persist} = {persist: false}) {
async function createSleepyLoggingLoader(log, {persist, forceParallel} = {persist: false, forceParallel: false}) {
const loader = new Loader({
// Этот "переход по маршруту" будет просто ждать нужное кол-во милилсекунд
// Ещё он может зависнуть, т.е. вернуть промис, который не разрезолвится никогда
Expand All @@ -24,6 +24,7 @@ async function createSleepyLoggingLoader(log, {persist} = {persist: false}) {
}
}, {
persist,
forceParallel,
processing(request, action, models) {
log.push(models.data);
}
Expand Down Expand Up @@ -246,5 +247,17 @@ describe('Loader', () => {
await loader.dispatch({timeout: 40, priority: Loader.PRIORITY_HIGH});

expect(log).toEqual(['timeout 40']);
})
});

test('dispatch high after low priority with forceParallel', async () => {
const log = [];
const loader = await createSleepyLoggingLoader(log, {forceParallel: true});

loader.dispatch({timeout: 20, priority: Loader.PRIORITY_LOW});
loader.dispatch({timeout: 10, priority: Loader.PRIORITY_HIGH});

await sleep(100);

expect(log).toEqual(['timeout 10', 'timeout 20']);
});
});