Skip to content

Commit

Permalink
refactoring: atomic remove object from Manager list
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorybesson committed Nov 1, 2016
1 parent 46dcf2b commit c945dfd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/cli/cms/data/file.js
Expand Up @@ -178,7 +178,7 @@ export function getFileObject(pathFile) {
const relativePath = pathFile.replace(pathData + '/', '')

const parentName = cmsData.fileAttr.delete(name)
const parentRelativePath = cmsData.fileAttr.delete(pathFile).replace(config.root, '').replace(/^\/?.+?\//, '')
const parentRelativePath = cmsData.fileAttr.delete(pathFile).replace(pathData + '/', '')

const fileData = cmsData.fileAttr.get(name)

Expand Down
3 changes: 1 addition & 2 deletions src/cli/cms/operations/remove.js
Expand Up @@ -12,7 +12,6 @@ import {


export function remove(filePath) {
filePath = coreUtils.slug.clean(filePath)
filePath = abeExtend.hooks.instance.trigger('beforeDeleteFile', filePath)

var revisions = cmsData.revision.getVersions(filePath)
Expand All @@ -21,7 +20,7 @@ export function remove(filePath) {
cmsOperations.remove.removeFile(revision.path, revision.htmlPath)
})

Manager.instance.removePostInList(filePath)
Manager.instance.removePostFromList(filePath)
}

export function removeFile(file, json) {
Expand Down
33 changes: 25 additions & 8 deletions src/cli/core/manager/Manager.js
Expand Up @@ -196,8 +196,12 @@ class Manager {
return this
}

/**
* When a new post is created, this method is called so that the manager updates the list with this new item
* @param {String} pathFile The full path to the post
*/
addPostInList(pathFile){
const parentRelativePath = cmsData.fileAttr.delete(pathFile).replace(config.root, '').replace(/^\/?.+?\//, '')
const parentRelativePath = cmsData.fileAttr.delete(pathFile).replace(this._pathData + '/', '')
const json = cmsData.file.get(pathFile)
let merged = {}
let rev = []
Expand All @@ -215,8 +219,12 @@ class Manager {
this._list.sort(coreUtils.sort.predicatBy('date', -1))
}

/**
* When a post is modified, this method is called so that the manager updates the list with the updated item
* @param {String} pathFile The full path to the post
*/
updatePostInList(pathFile){
const parentRelativePath = cmsData.fileAttr.delete(pathFile).replace(config.root, '').replace(/^\/?.+?\//, '')
const parentRelativePath = cmsData.fileAttr.delete(pathFile).replace(this._pathData + '/', '')
const found = coreUtils.array.find(this._list, 'parentRelativePath', parentRelativePath)
if(found.length > 0){
const index = found[0]
Expand All @@ -236,14 +244,23 @@ class Manager {
}
}

removePostInList(pathFile){
const parentRelativePath = cmsData.fileAttr.delete(pathFile).replace(config.root, '').replace(/^\/?.+?\//, '')
const found = coreUtils.array.find(this._list, 'parentRelativePath', parentRelativePath)
if(found.length > 0){
console.log('ok found')
}
/**
* When a post is deleted, this method is called so that the manager updates the list with the removed item
* @param {String} pathFile The full path to the post
*/
removePostFromList(pathFile){
let parentRelativePath = cmsData.fileAttr.delete(pathFile)
parentRelativePath = (pathFile.indexOf('/') === 0) ? parentRelativePath.substring(1):parentRelativePath
parentRelativePath = parentRelativePath.replace('.' + config.files.templates.extension, '.json')

this._list = coreUtils.array.removeByAttr(this._list, 'parentRelativePath', parentRelativePath)
}

/**
* This method loads all posts into an array with values used in "select" statements from templates
* + abe_meta data
* @return {Array} Array of objects representing the posts including their revisions
*/
updateList() {
this._list = cmsData.file.getAllWithKeys(this._whereKeys)
this._list.sort(coreUtils.sort.predicatBy('date', -1))
Expand Down
34 changes: 28 additions & 6 deletions src/cli/core/utils/array.js
@@ -1,19 +1,19 @@
/**
* Highly efficient filter on a value in an object
* @param {Array} arr the array of objects
* @param {string} key the key to filter on
* @param {string} attr the attribute to filter on
* @param {mixed} value the value to compare
* @return {Array} the filtered array
*/
export function filter(arr, key, value) {
export function filter(arr, attr, value) {
var result = []
var i = 0
var len = arr.length

for (; i < len; i += 1) {
var elt = arr[i]

if (elt[key] == value) {
if (elt[attr] == value) {
result.push(element)
}
}
Expand All @@ -23,21 +23,43 @@ export function filter(arr, key, value) {
/**
* Highly efficient find indexes on a value in an property of an object
* @param {Array} arr the array of objects
* @param {string} key the key to filter on
* @param {string} attr the attribute to filter on
* @param {mixed} value the value to compare
* @return {Array} the filtered array of indexes
*/
export function find(arr, key, value) {
export function find(arr, attr, value) {
var result = []
var i = 0
var len = arr.length

for (; i < len; i += 1) {
var elt = arr[i]

if (elt[key] == value) {
if (elt[attr] == value) {
result.push(i)
}
}
return result
}

/**
* Remove objects from an array given an attribute value
* @param {Array} arr the array of objects
* @param {string} attr the attribute to filter on
* @param {mixed} value the value to compare
* @return {Array} the array with corresponding objects removed
*/
export function removeByAttr(arr, attr, value){
var i = arr.length
while (i--){
if(
arr[i] &&
arr[i].hasOwnProperty(attr) &&
(arguments.length > 2 && arr[i][attr] === value )
){
arr.splice(i,1)
}
}

return arr
}

0 comments on commit c945dfd

Please sign in to comment.